package cn.ustb.serviceImpl;

import cn.ustb.dao.StudentBasicInfoDao;
import cn.ustb.entity.CriterStudent;
import cn.ustb.entity.StudentBasicInformation;
import cn.ustb.entity.User;
import cn.ustb.service.StudentBasicInfoService;
import cn.ustb.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author yuyan
 */
public class StudentBasicInfoServiceImpl implements StudentBasicInfoService {

    @Override
    public StudentBasicInformation get(User user) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentBasicInfoDao studentBasicInfoDao=sqlSession.getMapper(StudentBasicInfoDao.class);
            return studentBasicInfoDao.get(Long.parseLong(user.getUsername()));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public void update(StudentBasicInformation sbi) {
        SqlSession sqlSession=null;
        try{
            sqlSession = MyBatisUtil.getInstance();
            StudentBasicInfoDao studentBasicInfoDao=sqlSession.getMapper(StudentBasicInfoDao.class);
            studentBasicInfoDao.update(sbi);
            sqlSession.commit();
        }catch (Exception e){
            if(sqlSession!=null){
                sqlSession.rollback();
            }

        }finally {
            if(sqlSession!=null){
                sqlSession.close();
            }
        }
    }

    @Override
    public List<StudentBasicInformation> getForListWithCriterStudent(CriterStudent cs) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentBasicInfoDao studentBasicInfoDao=sqlSession.getMapper(StudentBasicInfoDao.class);
            return studentBasicInfoDao.getForListWithCriterStudent(cs);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }
}
